package com.gxa.admin.recycling.controller;

import com.gxa.admin.recycling.service.WasteTypeManagerService;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/waste")
@Api(tags = "物资分类设置")
public class WasteTypeController {

    @Autowired
    private WasteTypeManagerService wasteTypeManagerService=null;

    private List<ChangePointsStandard> list=new ArrayList<>();

    @PostMapping("/findAll")
    @ApiOperation("查询全部物资分类")
    public Map findAll(){
        List<WasteVo> all = wasteTypeManagerService.findAll();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(all);
        return jsonResult.getDataMap();
        //查询所有物资分类
        //查询废品父级,将查到的废品嵌套

    }
    @PostMapping("/findParentWasteType")
    @ApiOperation("查询全部父级分类")
    public Map findParentWasteType(){

        List<WasteType> parentWasteType = wasteTypeManagerService.findParentWasteType();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(parentWasteType);
        return jsonResult.getDataMap();


    }

    @PostMapping("/addChangePointsStandard")
    @ApiOperation("添加单个兑换标准")
    public void findManagerLevelId(@RequestBody ChangePointsStandard changePointsStandard,@ApiIgnore HttpSession session){

        list.add(changePointsStandard);
        session.setAttribute("list",list);

    }

    @PostMapping("/addWasteType")
    @ApiOperation("新增物资分类")
    public Map addWasteType(@RequestBody Waste waste,@ApiIgnore HttpSession session) throws BusinessException {

        List<ChangePointsStandard> list = (List<ChangePointsStandard>)session.getAttribute("list");

        wasteTypeManagerService.addWasteType(waste,list);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        //添加成功后删除
        session.removeAttribute("list");
        return jsonResult.getDataMap();
    }
    @PostMapping("/findWasteByTypeId")
    @ApiOperation("查询父级分类id查询所用的废品类别")
    public Map findWasteType(@RequestParam("wasteTypeId") Long wasteTypeId){
        List<Waste> wastes = wasteTypeManagerService.findWasteType(wasteTypeId);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(wastes);
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteWaste")
    @ApiOperation("删除废品类别")
    public Map delete(@RequestParam("wasteId") Long wasteId){

        wasteTypeManagerService.delete(wasteId);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();

    }
    @PostMapping("/updateState")
    @ApiOperation("修改状态")
    public Map updateState(@RequestParam("id") Long id, @RequestParam("state") Boolean state){
        Waste waste=new Waste();
        waste.setId(id);
        waste.setState(state);
        wasteTypeManagerService.updateState(waste);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();

    }


}
